package com.doubi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.doubi.entity.Commenttable;
import com.doubi.entity.Inform;
import com.doubi.entity.Releasetable;
import com.doubi.entity.Userinfo;
import com.doubi.service.CommenttableService;
import com.doubi.service.InformService;
import com.doubi.service.ReleasetableService;
import com.doubi.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@CrossOrigin("*")
@RestController
public class InformController {
    @Autowired
    private InformService informService;
    @Autowired
    private ReleasetableService releasetableService;
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private CommenttableService commenttableService;

    /**
     * 查询通知
     * @param uid 当前用户id
     * @param informType 通知类型，0：获赞 1：回复 2：系统通知 3：@我
     * @return 通知list
     */
    @GetMapping("/getInform")
    public List<Inform> getInform(Integer uid,Integer informType) {
        QueryWrapper<Inform> qw=new QueryWrapper<>();
        qw.eq("uid", uid).and(i->i.eq("informType", informType));
        List<Inform> list = informService.list(qw);
        for (int i=0;i<list.size();i++) {
            if (list.get(i).getReleaseId()!=0) {
                //视频信息
                QueryWrapper<Releasetable> qw1=new QueryWrapper<>();
                qw1.eq("releaseId", list.get(i).getReleaseId());
                list.get(i).setReleasetable(releasetableService.getOne(qw1));
                list.get(i).getReleasetable().setWorksCover("http://192.168.43.92:9000/star/"+list.get(i).getReleasetable().getWorksCover());
            }
            //发送通知人信息
            if (list.get(i).getBuid()!=0){
                QueryWrapper<Userinfo> qw2=new QueryWrapper<>();
                qw2.eq("userId",list.get(i).getBuid());
                list.get(i).setBUserinfo( userinfoService.getOne(qw2));
                list.get(i).getBUserinfo().setHeadImg("http://192.168.43.92:9000/star/"+list.get(i).getBUserinfo().getHeadImg());
            }
            //当前用户信息
            QueryWrapper<Userinfo> qw3=new QueryWrapper<>();
            qw3.eq("userId",list.get(i).getUid());
            list.get(i).setUserinfo(userinfoService.getOne(qw3));
            list.get(i).getUserinfo().setHeadImg("http://192.168.43.92:9000/star/"+list.get(i).getUserinfo().getHeadImg());
            if (list.get(i).getInformType()==3){
                //判断此评论是否@多人
                List<Inform> isManyPeople = informService.getIsManyPeople(list.get(i).getBUserinfo().getUserId(), list.get(i).getInformContent(), list.get(i).getInformTime(), list.get(i).getReleaseId());
                if (isManyPeople.size()>1){
                    int atIndex=list.get(i).getInformContent().indexOf("@");//获得第一个@下标
                    for (int j=0;j<isManyPeople.size();j++){
                        QueryWrapper<Userinfo> qw4=new QueryWrapper<>();
                        qw4.eq("userId",isManyPeople.get(j).getUid());
                        Userinfo thisP=userinfoService.getOne(qw4);
                        StringBuffer msb=new StringBuffer(list.get(i).getInformContent());
                        msb.replace(atIndex,atIndex+1, "@"+thisP.getUserName()+" ");
                        list.get(i).setInformContent(msb.toString());
                        atIndex = list.get(i).getInformContent().substring(atIndex+1).indexOf("@")+atIndex+thisP.getUserName().length()-1;
                    }
                }else {
                    //单人@
                    if (list.get(i).getInformContent()==null || list.get(i).getInformContent().equals("")){
                        list.get(i).setInformContent("@"+list.get(i).getUserinfo().getUserName()+" ");
                    }else {
                        StringBuffer sb=new StringBuffer(list.get(i).getInformContent());
                        sb.replace(list.get(i).getInformContent().indexOf("@"),list.get(i).getInformContent().indexOf("@")+1,"@"+list.get(i).getUserinfo().getUserName()+" ");
                        list.get(i).setInformContent(sb.toString());
                    }
                }
            }
            if (list.get(i).getInformType()==1){
                if (list.get(i).getReleaseId()==0){//说明回复的是评论
                    //获得评论内容
                    list.get(i).setCommenttable(commenttableService.getById(list.get(i).getCommentId()));
                }
            }
            if (list.get(i).getInformType()==0){
                if (list.get(i).getReleaseId()!=0){//说明点赞的是作品
                    list.get(i).setInformContent("点赞了你的作品");
                }else {//说明点赞的是评论
                    //获得评论内容
                    list.get(i).setCommenttable(commenttableService.getById(list.get(i).getCommentId()));
                    list.get(i).setInformContent("点赞了你的评论");
                }
            }
        }
        return list;
    }

    /**
     * 回复评论
     * @param inform 参数
     * @return 是否评论成功
     */
    @PostMapping("/requestInform")
    public String requestInform(@RequestBody Inform inform) {
        System.out.println(inform);
        Commenttable commenttable=new Commenttable();
        commenttable.setUserId(inform.getUserinfo().getUserId());
        commenttable.setReleaseId(inform.getReleaseId());
        commenttable.setCommentContent(inform.getInformContent());
        commenttable.setParentId(inform.getCommentId());
        commenttable.setStatus(1);
        LocalDateTime now=LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowTime=now.format(formatter);
        commenttable.setCommentTime(nowTime);
        //将回复信息存入回复表
        boolean save = commenttableService.save(commenttable);
        inform.setCommentId(commenttableService.getLastComment().getCid());
        int myUid=inform.getBuid();
        int bUid=inform.getUid();
        inform.setUid(myUid);
        inform.setBuid(bUid);
        inform.setInformType(1);
        inform.setId(0);
        inform.setInformTime(nowTime);
        //将回复信息做通知存入通知表
        boolean save1 = informService.save(inform);
        if (save1 && save){
            return "发送成功！";
        }else {
            return "发送失败！";
        }
    }


}
